<?php
define('IN_MOBILE', true);

global $_W,$_GPC;
wl_load()->model('setting');
wl_load()->model('member');
wl_load()->model('order');
wl_load()->model('group');
wl_load()->func('global');
wl_load()->func('pdo');
wl_load()->func('tpl');
wl_load()->func('message');
wl_load()->func('print');
$config = tgsetting_load();
$openid = getOpenid();
$controller = $_GPC['do'];
$action = $_GPC['ac'];
session_start();
$uniacid_tpl = pdo_fetch("select tpl,refund_type,last_refund_time,last_run_time from " . tablename('account_wechats') . " where  uniacid = '{$_W['uniacid']}'");
$function_tpl=pdo_fetch('select color from '.tablename('tg_function').' where id=:id',array(':id'=>$uniacid_tpl['tpl']));
$_W['system_color']=$function_tpl['color'];
$_W['uniacid_tpl']=$uniacid_tpl['tpl'];
$ip=getClientIP();
//if($ip == '112.16.53.66' || $ip == '116.62.157.59')
//{
//
//}else{
	if (empty($openid)) {
		die("<!DOCTYPE html>
            <html>
                <head>
                    <meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=0'>
                    <title>抱歉，出错了</title><meta charset='utf-8'><meta name='viewport' content='width=device-width, initial-scale=1, user-scalable=0'><link rel='stylesheet' type='text/css' href='https://res.wx.qq.com/connect/zh_CN/htmledition/style/wap_err1a9853.css'>
                </head>
                <body>
                <div class='page_msg'><div class='inner'><span class='msg_icon_wrp'><i class='icon80_smile'></i></span><div class='msg_content'><h4>请在微信客户端打开链接</h4></div></div></div>
                </body>
            </html>");
	}
//}


$member=pdo_fetch("SELECT id,total,from_user,parentid FROM ".tablename('tg_member')." WHERE from_user='{$_W['openid']}' and uniacid='{$_W['uniacid']}'");	
		
if((empty($member)||$member['parentid']==-1)&&!empty($_GPC['mid']))
{
			//message(intval($_GPC['sharenum']));
			if($member['parentid']==-1&&!empty($fans))
			{
				$anum=0;
			}else{
				$anum=intval($_GPC['mid']);
			}
			$data = array(					
					'parentid' => $anum
				);
			pdo_update('tg_member', $data, array('id' =>$member['id']));
			
}
$member=pdo_fetch("SELECT id,total,from_user,parentid,type,enable FROM ".tablename('tg_member')." WHERE from_user='{$_W['openid']}' and uniacid='{$_W['uniacid']}'");
if($member['parentid']!=99)	{
	$pmember=pdo_fetch("SELECT shopname FROM ".tablename('tg_member')." WHERE id='{$member['parentid']}' ");	
$_Session['btitle']=$pmember['shopname'];
}
if($member['type']==1 &&$member['enable']==1){
	$_Session['mid']=$member['id'];
}
checkMember();


$date_run=(TIMESTAMP-$uniacid_tpl['last_run_time'])/60;
$cancle_time = !empty($config['base']['cancle_time'])?$config['base']['cancle_time']:3;
$now_cancle_time=TIMESTAMP-$cancle_time*60;
$sql = "select  createtime,orderno from " . tablename('tg_order') . " where  uniacid = {$_W['uniacid']} and status = 0 and `createtime`<{$now_cancle_time} order by id asc limit 1";
$list1 = pdo_fetch($sql);
pdo_update('tg_order', array('status'=>9), array('orderno' => $list1['orderno']));

//报表基础数据实时更新
/*
$system_task=pdo_fetch("select * from cm_core_settings where cm_core_settings.key='task_time'");
$system_run=(TIMESTAMP-$system_task['value'])/60;
if($system_run>10)
{
	ihttp_request('http://www.lexiangpingou.cn/minapi.php?op=run_data_order',null,null,1);
	pdo_update('core_settings',array('value'=>TIMESTAMP),array('key'=>'task_time'));
}
*/



checkpay($openid);
if($_GPC[eid]==380)
{
	$controller = 'member';
	$action = 'home';
}
if($_GPC[eid]==382)
{
	$controller = 'order';
	$action = 'order';
}
if($_GPC[eid]==381)
{
	$controller = 'order';
	$action = 'mygroup';
}

if (empty($controller) || empty($action)) {
	$_GPC['do'] = $controller = 'goods';
	$_GPC['ac'] = $action = 'list';
}

$file = TG_APP . 'controller/'.$controller.'/'.$action.'.ctrl.php';
if (!file_exists($file)) {
	header("Location: index.php?i={$_W['uniacid']}&c=entry&do=goods&ac=list&m=lexiangpingou");
	exit;
}
if ($action == 'list' || $action == 'detail'||$action == 'personnelListDetail') {

	//pdo_delete('tg_collect', array('openid' =>$_W['openid'],'orderno'=>0));
}

require $file;

